﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace receiveSRV
{
    public partial class frmSRV : Form
    {
        public string IP = "192.168.1.199";
        public int port = 2345;
        string filepath = "";
        public frmSRV()
        {
            InitializeComponent();
        }

        private void cmdStart_Click(object sender, EventArgs e)
        {
            //sendReceive.buildConnectionstring(txtSqlServer.Text, txtSqlDatabase.Text, txtSqlUsername.Text, txtSqlPassword.Text);
            //if (!sendReceive.openDatabase())
            //{
            //    MessageBox.Show("Không kết nối được cơ sở dữ liệu");
            //    return;
            //}
            IP = txtIP.Text;
            int.TryParse(txtPort.Text, out port);
            cmdStart.Enabled = false;
            cmdStop.Enabled = true;
            //if (!sendReceive.checkSevice(IP, port))
            //{
            //    MessageBox.Show("Không mở được cổng để lắng nghe");
            //}
            if (!sendReceive.startListen(IP, port))
            {
                MessageBox.Show("Không mở được cổng để kết nối");
            }
            else
            {
                txtOut.Text += DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + " - Start\t\n";
            }
        }

        private void cmdStop_Click(object sender, EventArgs e)
        {

            txtOut.Text +=  DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + " - Stop\t\n";
            sendReceive.stopListen();
            cmdStart.Enabled = true;
            cmdStop.Enabled = false;
            //sendReceive.closeDatabase();
        }

        private void frmSRV_Load(object sender, EventArgs e)
        {
//            IPHostEntry host;
//            string localIP = "";
//            host = Dns.GetHostEntry(string.Empty);
//            foreach (IPAddress ip in host.AddressList)
//            {
//                if (ip.AddressFamily == AddressFamily.InterNetwork)
//                {
//                    localIP = ip.ToString();
////                    break;
//                }
//            }
            filepath = Process.GetCurrentProcess().MainModule.FileName;
            int l = filepath.LastIndexOf('\\');
            if (l > 0)
            {
                filepath = filepath.Substring(0, l + 1);
            }

            Directory.SetCurrentDirectory(filepath);
            StreamReader sr = new StreamReader(filepath+"server.txt");
            string IP = sr.ReadLine();
            string sport = sr.ReadLine();
            sr.Close();
            int port = 2345;
            int.TryParse(sport, out port);
            if (port == 0)
            {
                port = 2345;
            }

            txtIP.Text = IP;
            txtPort.Text = port.ToString();
            cmdStart_Click(sender, e);
            //ServiceReference1.CompositeType
            //ServiceReference1.Service1Client sv = new ServiceReference1.Service1Client();
            //ServiceReference1.CompositeType a = new ServiceReference1.CompositeType();
            //localService.Service1Client cl = new localService.Service1Client();
            //localService.dataobject b = new localService.dataobject();
            
            

            //txtOut.Text +=  DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + " - Stop\t\n";
        }

        private void frmSRV_FormClosed(object sender, FormClosedEventArgs e)
        {
//            sendReceive.closeDatabase();
            sendReceive.stopListen();
        }
    }
}
